/*
 * DJSet.h
 *
 *  Created on: Sep 15, 2012
 *      Author: jeffrey
 */

#ifndef DJSET_H_
#define DJSET_H_
#include<iostream>
#include<list>

namespace structure {

class DJSet {
public:
	DJSet(int value);
	virtual ~DJSet();
	DJSet& findSet() const;
	void print() {
		std::cout << value << " "<<rank<<std::endl;
	}
	static DJSet& makeUnion(DJSet& d1, DJSet& d2) {
		DJSet& p1 = (DJSet&) (d1.findSet());
		DJSet& p2 = (DJSet&) (d2.findSet());
		if (&p1 != &p2) { // if d1 , d2 belong to the different union
			if (p1.rank < p2.rank) {
				p1.parent = &p2;
				return p2;
			} else if (p1.rank >= p2.rank) {
				p2.parent = &p1;
				if (p1.rank == p2.rank)
					p1.rank++;
				return p1;
			}

		}
	}
private:
	int rank;
	int value;
	DJSet *parent;
};

} /* namespace structure */
#endif /* DJSET_H_ */
